package com.yangjiangtao.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/OnlineServlet")
public class OnlineServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 创建session对象
        HttpSession session = req.getSession();
        // 得到当前作用域下的在线人数
        Integer number = (Integer) session.getServletContext().getAttribute("number");
        // 设置响应类型及编码
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().write("<h2>当前服务器在线人数：" + number + "</h2>");
        // 跳转到销毁Session的Servlet
        resp.getWriter().write("<a href='/ExitServlet'>退出Session</a>");
    }
}
